package com.awcall.iot.controller;


import com.awcall.iot.common.ResponseResult;
import com.awcall.iot.model.Bank;
import com.awcall.iot.model.Statements;
import com.awcall.iot.service.IBankService;
import com.awcall.iot.service.IStatementsService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.awcall.iot.common.BaseController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhui
 * @since 2018-03-21
 */
@RestController
@RequestMapping("/admin/bank")
public class BankController extends BaseController {

    @Autowired
    private IBankService iBankService;

    @Autowired
    private IStatementsService iStatementsService;

    @GetMapping("/index")
    public ResponseResult index(Bank bank, Page<Bank> page){

        EntityWrapper<Bank> entityWrapper = new EntityWrapper<>();

        if (StringUtils.isEmpty(bank.getName())){
            entityWrapper.like(Bank.NAME,bank.getName());
        }

        Page<Bank> bankPage = iBankService.selectPage(page, entityWrapper);

        for (Bank b : bankPage.getRecords()){
            EntityWrapper<Statements> statementsEntityWrapper = new EntityWrapper<>();
            statementsEntityWrapper.eq("bank_id",b.getId());
            statementsEntityWrapper.in("type",new Integer[]{1,2}).setSqlSelect("ifnull(sum(price), 0)");
            Double originTotalAmount = (Double) iStatementsService.selectObj(statementsEntityWrapper);
            String format = String.format("%.2f", originTotalAmount);
            b.setPrice(Double.valueOf(format));
        }

        return new ResponseResult<>(bankPage);
    }

    @GetMapping("/list")
    public ResponseResult list(Bank bank){

        EntityWrapper<Bank> entityWrapper = new EntityWrapper<>();

        if (StringUtils.isEmpty(bank.getName())){
            entityWrapper.like(Bank.NAME,bank.getName());
        }

        List<Bank> banks = iBankService.selectList(entityWrapper);

        return new ResponseResult<>(banks);
    }

    @GetMapping("/detail")
    public ResponseResult detail(Bank bank){

        Bank bank1 = bank.selectById();

        return new ResponseResult<>(bank1);
    }

    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody Bank bank){

        boolean insert = bank.insert();

        if (insert) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody Bank bank){

        boolean updateById = bank.updateById();

        if (updateById) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody Bank bank){

        boolean deleteById = bank.deleteById();

        if (deleteById) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

}
